package com.cs.beneficios.web.action;

import com.cs.beneficios.business.entity.RepBeneficioOtorgadoDAO;
import com.cs.beneficios.business.entity.VflexDAO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.SQLException;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class SelectCriteriaMainAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// ConnectionFactory cf = new
		// ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
		HttpSession session = request.getSession();
		UserVO user = (UserVO) session.getAttribute("userInfo");

		Collection cComunas = null;
		Collection cBarrios = null;
		Collection cZonas = null;
		Collection cProgramas = null;
		Collection cSubprogramas = null;

		VflexDAO objValorFlexDAO = null;
		RepBeneficioOtorgadoDAO objBeneOtor1DAO = null;

		String opcion = request.getParameter("strOption");

		Object obCodePrograma = (Object) request.getParameter("codePrograma");

		try {
			objValorFlexDAO = new VflexDAO(cf);
			cComunas = objValorFlexDAO.getVflex("COMUNA");
			cBarrios = objValorFlexDAO.getVflex("BARRIO");
			cZonas = objValorFlexDAO.getVflex("ZONA");
			cf.closeConnection(objValorFlexDAO.getConn());
			request.setAttribute("ListComunas", cComunas);
			request.setAttribute("ListBarrios", cBarrios);
			request.setAttribute("ListZonas", cZonas);
			if ((Object) opcion != null) {
				if (opcion.equals("zona")
						&& !request.getParameter("codeZona").equalsIgnoreCase(
								"NULO")) {
					String codeZona = request.getParameter("codeZona");
					objValorFlexDAO = new VflexDAO(cf);
					cComunas = objValorFlexDAO.getVflex("COMUNA", codeZona);
					cBarrios = objValorFlexDAO.getVflex("BARRIO");
					cf.closeConnection(objValorFlexDAO.getConn());
					request.setAttribute("ListComunas", cComunas);
					request.setAttribute("ListBarrios", cBarrios);
					request.setAttribute("ListZonas", cZonas);
					request.setAttribute("zona", codeZona);
				} else if (opcion.equals("comuna")) {
					String codeComuna = request.getParameter("codeComuna");
					String codeZona = request.getParameter("codeZona");
					String zona = "";
					if (codeZona.equals("") || codeZona.equals("nulo")) {
						objValorFlexDAO = new VflexDAO(cf);
						zona = objValorFlexDAO.buscarPadre(codeComuna);
						cf.closeConnection(objValorFlexDAO.getConn());
						request.setAttribute("zona", zona);
					} else {
						zona = codeZona;
						request.setAttribute("zona", codeZona);
					}
					objValorFlexDAO = new VflexDAO(cf);
					cComunas = objValorFlexDAO.getVflex("COMUNA", zona);
					if (!codeComuna.equals("nulo")) {
						cBarrios = objValorFlexDAO.getVflex("BARRIO",
								codeComuna);
					}
					cf.closeConnection(objValorFlexDAO.getConn());
					request.setAttribute("comuna", codeComuna);
					request.setAttribute("ListComunas", cComunas);
					request.setAttribute("ListBarrios", cBarrios);
					request.setAttribute("ListZonas", cZonas);

				} else if (opcion.equals("barrio")) {
					String codeComuna = request.getParameter("vflComuna");
					String codeZona = request.getParameter("codeZona");
					String codeBarrio = request.getParameter("codeBarrio");

					if (!codeBarrio.equals("nulo")) {
						objValorFlexDAO = new VflexDAO(cf);
						cBarrios = objValorFlexDAO.getVflex("BARRIO");
						request.setAttribute("ListBarrios", cBarrios);
						cComunas = objValorFlexDAO.getVflex("COMUNA");
						request.setAttribute("ListComunas", cComunas);
						String zona = "";
						String comuna = "";
						comuna = objValorFlexDAO.buscarPadre(codeBarrio);
						request.setAttribute("comuna", comuna);
						if (!comuna.equals(""))
							zona = objValorFlexDAO.buscarPadre(comuna);
						cf.closeConnection(objValorFlexDAO.getConn());
						request.setAttribute("zona", zona);
						request.setAttribute("barrio", codeBarrio);
					}
				}
			}

			// Proceso para Programas y Subprogramas
			objBeneOtor1DAO = new RepBeneficioOtorgadoDAO(cf);
			cProgramas = objBeneOtor1DAO
					.findProgramaByUserReport(user.getCode(),
							user.getUserGroupName(), user.getCodeArea());
			cf.closeConnection(objBeneOtor1DAO.getConn());
			if (cProgramas.size() > 0) {
				request.setAttribute("ListProgramas", cProgramas);
			} else {
				request.setAttribute("msg",
						"No se encontraron programas vigentes a la fecha..");
			}
			if (obCodePrograma != null) {
				String codpro = (String) obCodePrograma;
				if (!codpro.equals("nulo")) {
					try {
						RepBeneficioOtorgadoDAO objBeneOtor2DAO = new RepBeneficioOtorgadoDAO(
								cf);
						String strCodeProg = (String) obCodePrograma;
						cSubprogramas = objBeneOtor2DAO
								.findSubProgramaByUserReport(user.getCode(),
										user.getUserGroupName(), strCodeProg,
										user.getCodeArea());
						cf.closeConnection(objBeneOtor2DAO.getConn());
					} catch (SQLException e) {
						System.err.println("/******** TDSUB - cwsub");
						e.printStackTrace();
						System.err.println("TDSUB - cwsub ********/");
					}
					request.removeAttribute("msg");
					if (cSubprogramas.size() > 0) {
						request.setAttribute("ListSubprogramas", cSubprogramas);
					} else {
						request.setAttribute("msg",
								"No se encontraron subprogramas asociados a este programa que esten vigentes..");
					}

					request.setAttribute("programa", codpro);
				} else {
					request.setAttribute("msg", "");
					request.removeAttribute("ListSubprogramas");
				}
			}
			// Fin del Proceso
		} catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
		}
		return mapping.findForward("success");
	}
}